﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using MM.Common.Base;
using System.Windows.Forms;
using MM.Common.DBBase;
using MM.Common.Setup;
using MM.Common.UI.Controls;
using System.ComponentModel;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Repository;
using System.Data;
using System.Drawing;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.Utils;
using System.Drawing.Drawing2D;
using DevExpress.XtraEditors.Controls;

namespace MM.Common.Support
{
    public partial class FrmComputerSetup : FrmSetupWithItem
    {
        public FrmComputerSetup()
        {
            InitializeComponent();
            base.gvItem = this.gvRole;
            base.gvList = this.gvComputer;
        }

        protected override void changeMode(bool edit)
        {
            base.changeMode(edit);
            base.pgBase.Enabled = true;
            this.txtComputerName.Properties.ReadOnly = !edit;
            this.txtIp.Properties.ReadOnly = !edit;
            this.txtAddress.Properties.ReadOnly = !edit;
            this.txtLocation.Properties.ReadOnly = !edit;
            this.meDescribe.Properties.ReadOnly = !edit;
            this.ckRemoteLogin.Properties.ReadOnly = !edit;
            this.cbDept.Properties.ReadOnly = !edit;
            base.nbiNew.Visible = false;
            this.txtComputerCode.Properties.ReadOnly = true;
        }

        protected override void deleteRecord()
        {
            if ((this.gvComputer.FocusedRowHandle >= 0) && (XtraMessageBox.Show("是否真的删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
            {
                DataRow focusedDataRow = base.gvList.GetFocusedDataRow();
                string paramValue = focusedDataRow["COMPUTER_CODE"].ToString();
                string[] commandtexts = new string[2];
                DataParamCol[] parms = new DataParamCol[2];
                commandtexts[0] = "DELETE MXHLY_SYS_COMPUTER_ROLE WHERE COMPUTER_CODE = :code ";
                parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("code", paramValue) });
                commandtexts[1] = "DELETE MXHLY_SYS_COMPUTER WHERE COMPUTER_CODE = :code ";
                parms[1] = new DataParamCol(new DataParam[] { new InputDataParam("code", paramValue) });
                switch (SystemInfo.DbData.RunSql(commandtexts, parms))
                {
                    case 0:
                        base.dtList.Rows.Remove(focusedDataRow);
                        base.gvList.Focus();
                        this.freshDisp();
                        return;

                    case 1:
                        XtraMessageBox.Show("角色删除时不成功!\n" + SystemInfo.DbData.ErrorMessage);
                        return;
                }
                XtraMessageBox.Show("机器删除时不成功!\n" + SystemInfo.DbData.ErrorMessage);
            }
        }

        protected override void freshDisp()
        {
            DataRow focusedDataRow = this.gvComputer.GetFocusedDataRow();
            if (focusedDataRow != null)
            {
                this.txtComputerCode.Text = focusedDataRow["COMPUTER_CODE"].ToString();
                this.txtComputerName.Text = focusedDataRow["COMPUTER_NAME"].ToString();
                this.txtIp.Text = focusedDataRow["IP"].ToString();
                this.txtAddress.Text = focusedDataRow["ADDRESS"].ToString();
                this.txtLocation.Text = focusedDataRow["LOCATION"].ToString();
                this.meDescribe.Text = focusedDataRow["DESCRIBE"].ToString();
                this.ckRemoteLogin.Checked = focusedDataRow["REMOTE_LOGIN"].ToString() == "Y";
                this.cbDept.EditValue = focusedDataRow["DEPT_ID"];
                string commandtext = "SELECT R.ROLE_CODE ,R.ROLE_NAME FROM MXHLY_SYS_COMPUTER_ROLE N ,MXHLY_SYS_ROLE R \r\n   WHERE N.ROLE_CODE = R.ROLE_CODE and N.COMPUTER_CODE = :node \r\n   ORDER BY R.ROLE_CODE ";
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("node", this.txtComputerCode.Text) });
                base.dtItem.Rows.Clear();
                SystemInfo.DbData.Retrieve(commandtext, parm, ref this.dtItem);
                base.dtItem.PrimaryKey = new DataColumn[] { base.dtItem.Columns["ROLE_CODE"] };
            }
        }

        protected override void getChoiceItem()
        {
            int[] numArray = new int[2];
            numArray[1] = 100;
            new FrmMulSelFromGrid(base.dtItemAll, new string[] { "代码", "名称" }, numArray, "机器角色选择", ref this.dtItem).ShowDialog(this);
        }

        protected override void getItemAll()
        {
            base.dtItemAll.Clear();
            SystemInfo.DbData.Retrieve("SELECT ROLE_CODE ,ROLE_NAME FROM MXHLY_SYS_ROLE ORDER BY ROLE_CODE", ref this.dtItemAll);
            base.dtItemAll.PrimaryKey = new DataColumn[] { base.dtItemAll.Columns["ROLE_CODE"] };
        }

        private void gvComputer_DoubleClick(object sender, EventArgs e)
        {
            if (base.nbiChoice.Visible)
            {
                this.getChoiceItem();
            }
        }

        public override bool Init()
        {
            bool flag = false;
            if (base.Init())
            {
                DataTable dt = new DataTable();
                SystemInfo.DbData.Retrieve("SELECT DEPT_ID ,DEPT_CODE ,DEPT_NAME ,INPUTCODE1 ,INPUTCODE2 FROM MXHLY_DIC_DEPT ORDER BY DEPT_CODE", ref dt);
                this.cbDept.Properties.DataSource = dt;
                this.cbDept.Properties.DisplayMember = "DEPT_NAME";
                this.cbDept.Properties.ValueMember = "DEPT_ID";
                base.dtList = new DataTable();
                SystemInfo.DbData.Retrieve("SELECT * FROM MXHLY_SYS_COMPUTER ORDER BY COMPUTER_CODE", ref this.dtList);
                base.dtList.TableName = "MXHLY_SYS_COMPUTER";
                base.nbiChoice.Caption = "角色";
                base.InitData();
                this.cbDept.ExistInputCode = true;
                flag = true;
            }
            return flag;
        }

        protected override bool saveRecord()
        {
            string text = this.txtComputerCode.Text;
            string paramValue = this.txtComputerName.Text;
            string str3 = this.txtIp.Text;
            string str4 = this.txtAddress.Text;
            string str5 = this.txtLocation.Text;
            string str6 = this.meDescribe.Text;
            string str7 = this.ckRemoteLogin.Checked ? "Y" : "N";
            int num = 0;
            if (this.cbDept.EditValue.ToString() != "")
            {
                num = Convert.ToInt16(this.cbDept.EditValue);
            }
            int index = 0;
            string[] commandtexts = new string[this.gvRole.RowCount + 2];
            DataParamCol[] parms = new DataParamCol[this.gvRole.RowCount + 2];
            commandtexts[0] = "UPDATE MXHLY_SYS_COMPUTER SET COMPUTER_NAME = :computername ,IP = :ip ,DEPT_ID = :dept_id ,\r\n     ADDRESS = :address ,LOCATION = :location ,DESCRIBE = :describe ,REMOTE_LOGIN = :remote_login \r\n     WHERE COMPUTER_CODE = :code";
            parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("code", text), new InputDataParam("computername", paramValue), new InputDataParam("ip", str3), new InputDataParam("dept_id", num), new InputDataParam("address", str4), new InputDataParam("location", str5), new InputDataParam("describe", str6), new InputDataParam("remote_login", str7) });
            commandtexts[1] = "DELETE FROM MXHLY_SYS_COMPUTER_ROLE WHERE COMPUTER_CODE = :code";
            parms[1] = new DataParamCol(new DataParam[] { new InputDataParam("code", text) });
            index = 2;
            int rowHandle = 0;
            while (rowHandle < this.gvRole.RowCount)
            {
                DataRow dataRow = this.gvRole.GetDataRow(rowHandle);
                commandtexts[index] = "INSERT INTO MXHLY_SYS_COMPUTER_ROLE (COMPUTER_CODE ,ROLE_CODE) VALUES (:code ,:rolecode)";
                parms[index] = new DataParamCol(new DataParam[] { new InputDataParam("code", text), new InputDataParam("rolecode", dataRow["ROLE_CODE"].ToString()) });
                rowHandle++;
                index++;
            }
            if (SystemInfo.DbData.RunSql(commandtexts, parms) == 0)
            {
                DataRow focusedDataRow = this.gvComputer.GetFocusedDataRow();
                focusedDataRow["COMPUTER_NAME"] = paramValue;
                focusedDataRow["DEPT_ID"] = num;
                focusedDataRow["IP"] = str3;
                focusedDataRow["ADDRESS"] = str4;
                focusedDataRow["LOCATION"] = str5;
                focusedDataRow["DESCRIBE"] = str6;
                focusedDataRow["REMOTE_LOGIN"] = str7;
                return true;
            }
            XtraMessageBox.Show("更新不成功！\n原因：" + SystemInfo.DbData.ErrorMessage, "出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            return false;
        }
    }
}
